<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.DICcaiZhiChaKan"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
    <script src="lib/js/html5shiv.min.js"></script>
    <script src="lib/js/respond.min.js"></script>
<![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1><spring:message code="message.lable.DICcaiZhiChaKan"/></h1>
        <ol class="breadcrumb">
               <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.DicManage"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.drugmaterialDic"/></a></li>
        </ol>
    </section>
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
            <form action="${ctx}/dicDrugmaterial/exportExcel.html" id="exportExcel" method="post">
            <input id="fileName" name="fileName" type="hidden" value="<spring:message code="message.lable.drugmaterialxls"/>" />
            <div class="input">
            
            		<div class="textalign1"><spring:message code="message.listForm.caiZhiMingCheng"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="txtDrugForm" name="materialName" placeholder="<spring:message code="message.listForm.caiZhiMingCheng"/>" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.caiZhiBianMa"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="txtDrugName" name="materialCode" placeholder="<spring:message code="message.listForm.caiZhiBianMa"/>" />
                    </div>
                    
                    <div class="textalign1"><spring:message code="message.lable.status"/>：</div>
	                  <div class="boxinput">
                        <select id="status" name="status" class="textinput">
                            	<option value="" ><spring:message code="message.select.option"/></option>
	                            <option value="1" ><spring:message code="message.button.start"/></option>
	                            <option value="0" ><spring:message code="message.button.stop"/></option>
                            </select>
                    </div>
                    <div class="textalign" >
                    <button type="button" onclick="likeQuery();" class="btn btn-success btn-sm" id="search"> <spring:message code="message.button.seachSpacing"/></button>
                </div>
                 </div>
                 </form>
            </div>
        </div>
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
           <ms:btn url="/dicDrugmaterial/add.html"><label id="addNew" class="btn btn-default btn-sm"><spring:message code="message.button.addSpacing"/></label></ms:btn>
           <ms:btn url="/dicDrugmaterial/enable.html"><label id="allStart" class="btn btn-default btn-sm"><spring:message code="message.button.startSpacing"/></label></ms:btn>
           <ms:btn url="/dicDrugmaterial/disable.html"><label id="allDelete" class="btn btn-default btn-sm"><spring:message code="message.button.stopSpacing"/></label></ms:btn>
            <label  onclick="exportExcel();" class="btn btn-default btn-sm"><spring:message code="message.lable.daoChu"/></label>
        </div>
        <div class="modal fade" id="exampleModal" tabindex="-1" >
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" ><span >&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel"><spring:message code="message.lable.DICcaiZhiXinZeng"/></h5>
                    </div>
                    <div class="modal-body">
                     <form>
                     <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label"><spring:message code="message.listForm.caiZhiBianMa"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="recipient-code" placeholder="<spring:message code="message.listForm.caiZhiBianMa"/>" >
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label"><spring:message code="message.listForm.caiZhiMingCheng"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="recipient-name" placeholder="<spring:message code="message.listForm.caiZhiMingCheng"/>" >
                        </div>
                    </div>
                    </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="save();" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></button>
                        <button type="button" class="btn btn-danger btn-sm" data-dismiss="modal"><spring:message code="message.button.closeSpacing"/></button>
                    </div>
                </div>
            </div>
        </div>
    </section>
   <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
	    //绑定回车
	    $('#txtDrugName,#txtDrugForm,#status').bind('keydown', function(event) {
	        if (event.keyCode == "13") {
	            $('#search').click();
	        }
	    });	
  
    	var id = "";
    	var status = "";
        $(function () {
        	
        	$("#txtDrugForm").focus();//加载页面光标在第一个查询框内
        	
        	$('#allDelete').click(function(){
        		batchChangeStatus(0);
        	});
        	$('#allStart').click(function(){
        		batchChangeStatus(1);
        	});
        	//新增
            $('#addNew').click(function () {
            	$('.modal-title').html('<spring:message code="message.lable.DICcaiZhiXinZeng"/>');
            	id="";
            	status="";
            	$('#recipient-name').val("");
				$('#recipient-code').val("");
                $('#exampleModal').modal({
                    keyboard: true
                });
            });
          
            $("#gridlist").jqGrid({
	                url: "dicDrugmaterialList.html",
	                mtype: "POST",
	                datatype: "json",
	                autowidth: true,
	                height: 270,
	                colNames: ['<spring:message code="message.jqGrid.cz"/>',
	                           '',
	                           '<spring:message code="message.listForm.caiZhiBianMa"/>',
	                           '<spring:message code="message.listForm.caiZhiMingCheng"/>',
	                           '<spring:message code="message.lable.status"/>'
	                           ],
	                colModel: [
						{ name: 'operate', width: 40,sortable: false, align:"center",
							formatter : function(val, opts, rowdata)
							{
								var str =  "<ms:btn url='/dicDrugmaterial/edit.html'><a href=\"javascript:edit('"+rowdata.materialName+"','"+rowdata.materialCode+"','"+rowdata.materialId+"','"+rowdata.status+"');\"  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.bianJi"/>'></a></ms:btn>";
								str += "<ms:btn url='/dicDrugmaterialLog/log.html'><a href='${ctx}/dicDrugmaterialLog/log.html?materialId="+rowdata.materialId+"&materialCode="+rowdata.materialCode+"&materialName="+rowdata.materialName+"'  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.weiHuJiLu"/>'></a></ms:btn>";
								return str;
						}},
	                    { name: 'materialId', index: 'materialId', hidden:true, width: 80, align:"center", sortable: false},
	                    { name: 'materialCode', index: 'materialCode', width: 80, align:"center", sortable: false},
	                    { name: 'materialName', index: 'materialName', width: 150, align:"center", sortable: false },
	                    { name: 'status', index: 'status', width: 30 , align: 'center', sortable: false,
	                    	formatter : function(val, opts, rowdata) {
	                    		var strSta = "";
	                    		if(rowdata.status == 1){
	                    			strSta += "<spring:message code="message.button.start"/>";
	                    		}else{
	                    			strSta += "<span class='red'><spring:message code="message.button.stop"/></span>";
	                    		}
	                    		return strSta;
	                    	}
	                    }
	                       
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                viewrecords: true,
                multiselect: true, 
                gridComplete: function(){
                	var page = $("#gridlist").jqGrid('getGridParam','page');
            		var rows = $('#gridlist').getGridParam("rowNum");
            		var maxnum = (rows*page).toString();
            		var len = (maxnum.length)*10;
            		$('.jqgfirstrow').find('td').eq(0).css('width',len+'px');
                	$('#gridlist_rn').css('width',len+'px');
                	
                	//单条数据自动选中
                	var rowIds = $("#gridlist").jqGrid('getDataIDs');
                	if(rowIds.length==1){
                		$("#"+rowIds[0]).click();
                	}
                },
                caption: "<spring:message code="message.list.drugmaterialDiclist"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "materialId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
        });
       
        //停用、启用
        function batchChangeStatus(status) {
			var msg = "<spring:message code="message.button.stop"/>";
			var url = "${ctx}/dicDrugmaterial/enable.html";
			if(status == 1){
				msg = "<spring:message code="message.button.start"/>";
				url = "${ctx}/dicDrugmaterial/disable.html";
			}
			var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
			if (rowIds.length <= 0) {
				$.alert("<spring:message code="message.lable.pleaseSelectOperational"/><spring:message code="message.listForm.caiZhi"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
				return;
			}
			
			var data = "";
			var list = [];
			for(var i = 0;i<rowIds.length;i++){
				var sta =  $("#gridlist").jqGrid('getRowData',rowIds[i]);
				if(sta.status != msg){
					var materialName = sta.materialName;
					
					data += materialName+",";
					list.push({"materialId":rowIds[i], "status":status});
				}else{
					
				}  
			}
			if(data){
				data = data.substring(0,data.length-1);
			}else{
				$.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.lable.OKData"/>", "warn");
				return;
			}
			
			$.HN.message.confirm("<spring:message code="message.lable.OKYES"/>"+msg+"<spring:message code="message.lable.FlowerDataYESorNo"/><br/>"+data, '', '').on(function (e) {
	            if (e) {
			$.post(url, {
				"array" : json.jsonToString(list)
			}, function(result) {
				if (result.success) {
					var page = $("#gridlist").jqGrid('getGridParam','page');
					$.alert("<spring:message code="message.HN.alert.chengGongCaoZuo"/>"+result.conditions.sum+"<spring:message code="message.HN.alert.tiaoShuJu"/>", "success");
					likeQuery(page);
				} else {
					$.alert(result.msg ||"<spring:message code="message.HN.alert.fail"/>", "error");
				}
			}, "json");
	            }
			});
		}
       
		//保存
        function save(){
        	 if (!validate()) {
	    			return;
	    	}
     	 var materialName = $("#recipient-name").val();
     	 var materialCode = $("#recipient-code").val();
      	     $.HN.message.confirm('<spring:message code="message.HN.alert.OkSaveYESorNO"/>', '', '').on(function (e) {
                if (e) {
                	var url = "${ctx}/dicDrugmaterial/add.html";
                    if(id !=null && id != ""){       
                    	url = "${ctx}/dicDrugmaterial/edit.html";    
                    	}
                	$.ajax({
               		   type: "POST",
               		   url: url,
               		   data: "materialCode="+materialCode+"&materialName="+materialName+"&materialId="+id+"&status="+status,
               		   dataType:"json",
               		   success: function(result){
               			   if(result.success){
               				$.alert("<spring:message code="message.HN.alert.success"/>", "success");
               				var page = $("#gridlist").jqGrid('getGridParam','page');
	           				   likeQuery(page);
	           				   $('#exampleModal').modal('hide');
               			   }else{
               				$.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
                			   }
                		   }
                	});
                }
            });
        }
       //编辑
        function edit(materialName,materialCode,materialId,status1){
        	    id = materialId;
        	    status=status1;
        	    $('.modal-title').html('<spring:message code="message.lable.DICcaiZhiBianJi"/>');
   				$('#recipient-name').val(materialName);
				$('#recipient-code').val(materialCode);
        		$('#exampleModal').modal({
               		 keyboard: true
           		 });
        }
        //查询
        function likeQuery(page){
        	var materialCode  = $("#txtDrugName").val();
       	    var materialName = $("#txtDrugForm").val();
       	    var status = $("#status").val();
       	    $("#gridlist").jqGrid('setGridParam', { 
       	    	url: "${pageContext.request.contextPath}/dicDrugmaterial/dicDrugmaterialList.html",
       	    	mtype:"post",
       	    	postData:{
					"materialCode":materialCode,
					"materialName":materialName,
					"status":status},
		        page: page||1
        }).trigger("reloadGrid");
        }
        //校验
        function validate() {
        	var materialName = $.trim($("#recipient-name").val());
       	 	var materialCode = $.trim($("#recipient-code").val());
       	 	if(materialCode == null || materialCode == ""){
       	 		$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.caiZhiBianMa"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
       	 		return false;
       	 	}
			if(materialCode.length>10){
				$.alert("<spring:message code="message.listForm.caiZhiBianMa"/><spring:message code="message.yanZheng.changDuGuoChang"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
    			return false;
			}
			if(materialName == null || materialName == ""){
       	 		$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.caiZhiMingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
       	 		return false;
       	 	}
			if(materialName.length>100){
				$.alert("<spring:message code="message.listForm.caiZhiMingCheng"/><spring:message code="message.yanZheng.changDuGuoChang"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
    			return false;
			}
    		return true;
    	}
        
        function exportExcel(){
     		$.HN.message.confirm('<spring:message code="message.lable.OKExport"/>&nbsp;<spring:message code="message.lable.drugmaterialxls"/>&nbsp;<spring:message code="message.lable.ma"/>', '', '').on(function (e) {
	               if (e) {
	            	  $("#exportExcel").submit();
			
	               }
          	 }); 
          	
     	}
        
    </script>
</body>
</html>